// 桌面图标区域
desktop.shortcut = {
	counter : [],
	render : function(shortcuts) {

		desktop.shortcut.counter = [];

		var panel = desktop.container.panel;
		var template = '<li class="shortcut" id="{id}" title="{title}">' + '<span class="icon"><img src="{image}" /><span class="msg_count"></span></span>' + '<div class="text">{text}<s></s></div></li>';
		$(shortcuts).each(function(index, item) {
			var p = $(template.replace('{id}', "id_" + item.id).replace('{title}', item.name).replace('{image}', item.icon_image).replace('{text}', item.name));
			if (item.index_num)
				$(panel.get(item.index_num - 1)).append(p);
			else
				$(panel.get(0)).append(p);
			if (item.count_url) {
				desktop.shortcut.counter.push({
					id : item.id,
					countUrl : item.count_url,
					countInterval : item.count_interval || 60
				});
			}
		});

		this.rendered = true;
	},

	// 桌面图标排列
	arrangeIcons : function(panel) {
		var myData = desktop.getMydata(), winWh = myData.winWh, navbar = desktop.navbar.element, navBarHeight = navbar.outerHeight()
		// 计算一共有多少图标
		, iconNum = panel.find("li").size();

		// 存储当前总共有多少桌面图标
		panel.data('deskIconNum', iconNum);

		var gH = 120; // 一个图标总高度，包括上下margin
		var gW = 120; // 图标总宽度,包括左右margin
		var rows = Math.floor((winWh['h'] - navBarHeight - 75) / gH);
		var curcol = 0, currow = 0;

		panel.find("li").css({
			"position" : "absolute",
			"margin" : 0,
			"left" : function(index, value) {
				var v = curcol * gW + 30;
				if ((index + 1) % rows == 0) {
					curcol = curcol + 1;
				}
				return v;
			},
			"top" : function(index, value) {
				var v = (index - rows * currow) * gH + 20;
				if ((index + 1) % rows == 0) {
					currow = currow + 1;
				}
				return v;
			}
		});
	},
	upDesktop : function(panel, meBlock, inner, meNum, navBarHeight, lBarWidth) {
		var myData = desktop.getMydata(), winWh = myData.winWh, w = winWh['w'] - lBarWidth, h = (winWh['h'] - 75 - navBarHeight), _this = this;

		// 设置桌面图标容器元素区域大小
		inner.css({
			"width" : ((w + lBarWidth) * meNum) + "px",
			"height" : h + "px"
		});
		panel.css({
			"width" : w + "px",
			"height" : h + "px",
			'margin-right' : lBarWidth
		});
		meBlock.css({
			"width" : w + "px",
			"height" : h + "px",
			"margin-top" : navBarHeight,
			'margin-left' : lBarWidth + 'px',
			'margin-bottom' : 75 + "px"
		});

		panel.each(function() {
			_this.arrangeIcons($(this));

			$(this).droppable({
				scope : 'a',
				drop : function(event, ui) {
					ui.draggable.addClass("shortcut").insertBefore($(this).find(".add_icon")).find("span").addClass("icon");
					_this.init();
					desktop.dockbar.init();
				}
			});
		});
	},
	desktopMove : function(inner, panel, navtab, dates, moveDx, nextIndex) {
		inner.css('left', 0);
		panel.eq(desktop.currentPanel).fadeOut(300, function() {
			desktop.currentPanel = nextIndex;
			panel.removeClass("current_panel").eq(nextIndex).addClass("current_panel");
			$('#indicatorContainer').removeClass('nav_current_' + (desktop.navbar.index + 1));
			desktop.navbar.index = nextIndex;
			$('#indicatorContainer').addClass('nav_current_' + (desktop.navbar.index + 1));
			panel.eq(desktop.currentPanel).fadeIn(300);
		});
		/*
		 * inner.stop().animate({ left : -(nextIndex) * moveDx }, dates,
		 * function() { panel.removeClass("current_panel").eq(nextIndex)
		 * .addClass("current_panel");
		 * 
		 * $('#indicatorContainer').removeClass('nav_current_' +
		 * (desktop.navbar.index + 1)); desktop.navbar.index = nextIndex;
		 * $('#indicatorContainer').addClass('nav_current_' +
		 * (desktop.navbar.index + 1)); });
		 */
	},
	init : function(iconData) {
		if (this.rendered !== true) {
			this.render(iconData);
		}
		var container = desktop.container.element, inner = desktop.container.inner, panel = desktop.container.panel, meNum = panel.size(), navbar = desktop.navbar.element, navBarHeight = navbar.outerHeight(), navtab = navbar.find("a"), lBarWidth = 73, me = this;

		me.upDesktop(panel, container, inner, meNum, navBarHeight, lBarWidth);

		// 如果窗口大小改变，则重新排列图标
		$(window).wresize(function() {
			desktop.winWH(); // 更新窗口大小数据
			me.upDesktop(panel, container, inner, meNum, navBarHeight, lBarWidth);
		});
		// 附加data数据
		desktop.iconDataInit(iconData);

		// 桌面可使用鼠标拖动切换
		var timeStart, timeEnd, dxStart, dxEnd;

		// 图标鼠标经过效果
		panel.find("li").hover(function() {
			$(this).addClass("shortcut_over");
		}, function() {
			$(this).removeClass("shortcut_over");
		}).not("li.add_icon").click(function(event) {
			var data = $(this).data("iconData"), id = this.id;

			if (data.url.indexOf('?') != -1) {
				data.url += "&dialogId=" + "d_" + id;
			} else {
				data.url += "?dialogId=" + "d_" + id;
			}

			desktop.win.newWin({
				title : data.name,
				src : data.url,
				id : "d_" + id,
				query : true,
				modal : data.modal
			});
		});

		var data = [ [ {
			text : "刷新桌面",
			func : function() {
				location.href = "desktop";
			}
		}, {
			text : "桌面设置",
			func : function() {
				desktop.win.newWin({
					title : '桌面设置',
					src : 'app/menu/webosSettingEdit?dialogId=desktopSetting',
					id : 'desktopSetting',
					width : 800,
					query : true
				});
			}
		}, {
			text : "切换至传统模式",
			func : function() {
				location.href = "main";
			}
		} ], [ {
			text : "我要搜索",
			func : function() {
				desktop.win.newWin({
					title : '我要搜索',
					src : 'knowledge/knowledgeSearch/searchResultView',
					id : 'query',
					query : true
				});
			}
		}, {
			text : "我的信息箱",
			func : function() {
				desktop.win.newWin({
					title : '我的信息箱',
					src : 'app/message/myMessageBoxView',
					id : 'myMessageBox',
					query : true
				});
			}
		} ], [ {
			text : "帮助中心",
			func : function() {
				desktop.win.newWin({
					title : '帮助中心',
					src : 'app/systemHelp/index/USER',
					id : 'helpdd',
					query : true
				});
			}
		} ], [ {
			text : "退出系统",
			func : function() {
				location.href = "j_spring_security_logout";
			}
		} ] ];
		desktop.contextMenu($(document.body), data, "body", 10);
	},
	initCounter : function() {
		var counterNum = 0;
		var counterFun = function() {
			counterNum++;
			$(desktop.shortcut.counter).each(function(index, item) {

				if (counterNum >= item.countInterval && counterNum % item.countInterval == 0) {
					$.get(item.countUrl, function(data) {
						if (data && data > 0) {
							$("#id_" + item.id + " span.msg_count").show();
							$("#id_" + item.id + " span.msg_count").text(data);
						} else {
							$("#id_" + item.id + " span.msg_count").hide();
						}
					});
				}
			});
		};
		$(desktop.shortcut.counter).each(function(index, item) {
			$.get(item.countUrl, function(data) {
				if (data && data > 0) {
					$("#id_" + item.id + " span.msg_count").show();
					$("#id_" + item.id + " span.msg_count").text(data);
				} else {
					$("#id_" + item.id + " span.msg_count").hide();
				}
			});
		});
		window.setInterval(counterFun, 1000);
	}
}